<?php

function php2js($dta) {
  if(is_object($dta))
    $dta = get_object_vars($dta);
  if(is_array($dta)) {
    foreach($dta AS $k=>$d)
      $dta[$k] = php2js($k).":".php2js($d);
    return '{'.implode(',',$dta).'}';
  }
  elseif(is_numeric($dta))
    return "$dta";
  elseif(is_string($dta))
    return '"'.str_replace('"','\\"',$dta).'"';
  else
    return 'null';
 }

function camelize($pString){
  $subs = explode(' ', $pString);
  $output = array();
  foreach($subs as $sub){
    $output[] = ucfirst(strtolower($sub));
  }
  return implode(' ', $output);
}

function formatCurrency($pNumber){
  $currency = new Zend_Currency('en_US');
  return $currency->toCurrency($pNumber);
}

function phptal_tales_empty($pSrc, $pNoThrow){
  $src = trim($pSrc);
  return "count(".PHPTAL_Php_TalesInternal::path($src, $pNoThrow).") == 0";
}

function phptal_tales_camelize($pSrc, $pNoThrow){
  $src = trim($pSrc);
  return 'camelize('.PHPTAL_Php_TalesInternal::path($src, $pNoThrow).')';
}

function phptal_tales_currency($pSrc, $pNoThrow){
  $src = trim($pSrc);
  return 'formatCurrency('.PHPTAL_Php_TalesInternal::path($src, $pNoThrow).')';
}

function phptal_tales_translate($pSrc, $pNoThrow){
  $src = trim($pSrc);
  return '$ctx->this->translate('.PHPTAL_Php_TalesInternal::string($src, $pNoThrow).')';
}

function phptal_tales_count($pSrc, $pNoThrow){
  $src = trim($pSrc);
  return 'count('.PHPTAL_Php_TalesInternal::path($src, $pNoThrow).')';
}

function phptal_tales_route($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return '$ctx->this->link(' . PHPTAL_Php_TalesInternal::string($src, $pNoThrow) . ')';
}

function phptal_tales_permission($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return '$ctx->this->hasPermission(' . PHPTAL_Php_TalesInternal::string($src, $pNoThrow) . ')';
}


function phptal_tales_pluralize($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return '$ctx->this->pluralize(' . PHPTAL_Php_TalesInternal::path($src, $pNoThrow) .')';
}

function phptal_tales_lower($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return 'strtolower(' . PHPTAL_Php_TalesInternal::path($src, $pNoThrow) . ')';
}

function phptal_tales_upper($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return 'strtoupper(' . PHPTAL_Php_TalesInternal::path($src, $pNoThrow) . ')';
}
?>